<?php
namespace Home\Controller;
use Think\Controller;

class IndexController extends Controller {
    
    //对于页面中的评论，在需要显示的地方 每个表都设置一个Comm字段比较合理，设置冗余字段，减少sql查询

    public function index(){
    	$artmodel = D('art');
    	$catmodel = D('cat');
        $commentmodel = D('comment');

        //首页的文章 和 分页
        $count = $artmodel->count();
        $Page = new \Think\Page($count,8);
        $show = $Page->show();
        $data = $artmodel->order('art_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $newdata=array();
        foreach ($data as $key => $value) {
            $a = $value['art_id'];
            $comment = $commentmodel->where('art_id='.$a)->select();
            $num = count($comment);
            $value['comm'] = $num;
            $catname = $catmodel->find($value['cat_id']);
            $value['cat_id']=$catname['catname'];
            $value['pubtime']=date("Y-m-d",$value['pubtime']);

            if (strlen($value['content'])>1000) {
                $value['content'] = mb_substr($value['content'],0,388,'utf-8').'......';
            }

            $newdata[]=$value;
        }

    	$this->assign('art',$newdata);
        $this->assign('page',$show);

        //右边的最新文章
        $right = $artmodel->order('pubtime desc')->limit(0,8)->select();
        $rightdata = array();
        foreach ($right as $key => $value) {
            $a = $value['art_id'];
            $comment = $commentmodel->where('art_id='.$a)->select();
            $num = count($comment);
            $value['comm'] = $num;
            $rightdata[]=$value;
        }
        

        $this->assign('hot',$rightdata);

        //导航栏
        $top = $catmodel->order('cat_id asc')->select();
        $this->assign('top',$top);

        //天气信息
        $data = 'theCityName=北京';
        $curlobj = curl_init(); 
        curl_setopt($curlobj, CURLOPT_URL, "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName");  
        //curl_setopt($curlobj, CURLOPT_HEADER, 0); 
        curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);  
        curl_setopt($curlobj, CURLOPT_POST, 1);  
        curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data);
        curl_setopt ($curlobj, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);  //关于HTTP协议所需要的部分要详细看 
        curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8", 
            "Content-length: ".strlen($data)
            ));
        $rtn = curl_exec($curlobj);
        curl_close($curlobj);

        $weather=$this->xmlToArray($rtn);

        $this->assign('weather',$weather);


        //右侧的登录头像可以替换
        if($_COOKIE['uid']){
            $usermodel = D('user');
            $userdata = $usermodel -> find($_COOKIE['uid']);
            $this->assign('pic',$userdata);
        }


        $this->display();
        
    }



    public function art(){
       
        $artmodel = D('art');
        $catmodel = D('cat');
        $commentmodel = D('comment');

        $data = $artmodel->find($_GET['art_id']);
        $data['pubtime']= date("Y-m-d H:i:s",$data['pubtime']);
        $a = $catmodel->find($data['cat_id']);
        $data['cat_id'] = $a['catname'];
        $this->assign('data',$data);

        $commentmodel = D('comment');
        $comment = $commentmodel->select();
        $arr = array();
        foreach ($comment as $key => $value) {
            if($value['art_id']==$_GET['art_id']){
                $value['pubtime']=date('Y-m-d H:i:s',$value['pubtime']);
                $arr[]=$value;
            }
        }
        $this->assign('comment',$arr);

        //导航栏
        $top = $catmodel->order('cat_id asc')->select();
        $this->assign('top',$top);

        //右边的最新文章
        $right = $artmodel->order('pubtime desc')->limit(0,8)->select();

        $rightdata = array();
        foreach ($right as $key => $value) {
            $a = $value['art_id'];
            $comment = $commentmodel->where('art_id='.$a)->select();
            $num = count($comment);
            $value['comm'] = $num;
            $rightdata[]=$value;
        }

        $this->assign('hot',$rightdata);

        $this->display();
    
    }

    public function comment(){
        $comment = D('comment');
        $usermodel = D('user');

        $data = $usermodel->find($_COOKIE['uid']);

        $_POST['pubtime']=time();
        $_POST['pic'] = $data['pic'];
        $_POST['user_id'] = $data['user_id']; 
        $Result= $comment->add($_POST);

        $this->redirect('home/index/art',array('art_id'=>$_POST['art_id']));
        
    }


    public function xmlToArray($xml){    
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);        
        return $values;
    }

    public function nav(){
        if($_GET){
        $artmodel = D('art');
        $catmodel = D('cat');
        $commentmodel = D('comment');

        //导航栏
        $top = $catmodel->order('cat_id asc')->select();
        $this->assign('top',$top);

        //分类后的文章
        $data = $artmodel -> select();
        $newdata=array();
        foreach ($data as $key => $value) {
            $a = $value['art_id'];
            $comment = $commentmodel->where('art_id='.$a)->select();
            $num = count($comment);
            $value['comm'] = $num;
            $catname = $catmodel->find($value['cat_id']);
            $value['cat_id']=$catname['catname'];
            $value['pubtime']=date("Y-m-d",$value['pubtime']);
            $newdata[]=$value;
        }

        $last=array();
        foreach ($newdata as $key => $value) {
            if ($value['cat_id']==$_GET['catname']) {
                $last[]=$value;
            }  
        }

        $this->assign('last',$last);

        //天气信息
        $data = 'theCityName=北京';
        $curlobj = curl_init(); 
        curl_setopt($curlobj, CURLOPT_URL, "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName");  
        //curl_setopt($curlobj, CURLOPT_HEADER, 0); 
        curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);  
        curl_setopt($curlobj, CURLOPT_POST, 1);  
        curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data);
        curl_setopt ($curlobj, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);  //关于HTTP协议所需要的部分要详细看 
        curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8", 
            "Content-length: ".strlen($data)
            ));
        $rtn = curl_exec($curlobj);
        curl_close($curlobj);

        $weather=$this->xmlToArray($rtn);

        $this->assign('weather',$weather);


        //右边的最新文章
        $right = $artmodel->order('pubtime desc')->limit(0,8)->select();
        $rightdata = array();
        foreach ($right as $key => $value) {
            $a = $value['art_id'];
            $comment = $commentmodel->where('art_id='.$a)->select();
            $num = count($comment);
            $value['comm'] = $num;
            $rightdata[]=$value;
        }
        

        $this->assign('hot',$rightdata);


        $this->display();
        }
        
    }


    public function pic(){
        
        if(!$_FILES){

            echo 1;

        }else{
       

        $upload = new \Think\Upload();
        $upload -> exts = array('jpg','gif','png','jpeg');
        $upload -> rootPath = 'Public/picture/';  //这里需要的路径一定要自己创建TP不会自动生成

        $info = $upload->upload();  //实际上图片通过表单就存在了浏览器上，然后TP通过这个方法存到本地。
        //这里的 $info就是这个图片的信息 是个数组。 我们就不需要考虑 moved_upload... $_FILES 这些了
        //TP都写好了 不要多想


        if(!$info){
                var_dump($upload->getError());
            }else{

                $_POST['ori_img'] = 'Public/picture/'.$info['picture']['savepath'].$info['picture']['savename'];
                
                //图片缩放
                $img = new \Think\Image();
                $img -> open($_POST['ori_img']); //需要缩放的图的路径
                $thumb_path = './Public/picture/thumb/'.$info['picture']['savename'];
                $img -> thumb(60,60)->save($thumb_path); //图片缩小多少，放到哪

                $_POST['thumb_img'] = $thumb_path;
                
                
            }

        $thumb_path = strstr($thumb_path,"/");
        $usermodel = D('user');
        $usermodel -> find($_COOKIE['uid']);
        $usermodel -> pic = $thumb_path;
        $usermodel -> save();
        $this->redirect('home/index/index');
        }

    }





    
}